package cn.edu.scau.cmi.yejiancong.ORM.client;

import java.io.IOException;
import java.util.List;

import org.hibernate.sql.Select;

import cn.edu.scau.cmi.yejiancong.ORM_hibernate.DAO.studentDAO;
import cn.edu.scau.cmi.yejiancong.ORM_hibernate.DAO.teacherDAO;
import cn.edu.scau.cmi.yejiancong.ORM_hibernate.entiry.Student;
import cn.edu.scau.cmi.yejiancong.ORM_hibernate.entiry.Teacher;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.Label;

import javafx.scene.control.TreeView;
import javafx.stage.Stage;

public class seeviewController {
	@FXML
	private TreeView<String> treeview=new TreeView<>();
	@FXML
	private Label studentid=new Label();
	public static String studentids="";
	@FXML
	private Label studentname=new Label();
	@FXML
	private Button addbtn;
	@FXML
	private Button detailbtn;
	@FXML
	private Button delbtn;
	@FXML
	private TextField addnametext;
	private int Selectstudentorteacher=-1;
	private List<Student> Slist;
	private List<Teacher> Tlist;
	private TreeItem<String> item;
	//初始化，构树表，
	@FXML
	public void initialize() {
		Selectstudentorteacher=client.select;
		
		 item=new TreeItem<>("list");
		 item.setExpanded(false);
		 TreeItem<String> sItem=new TreeItem<>();
		 
		 
		 treeview.setRoot(item);
		//0是学生
		 if(Selectstudentorteacher==0) {
			 int index=0;
			 Slist=studentDAO.getall();
			 for(Student s:Slist) {
				 
				  sItem=new TreeItem<String>(s.getStudentid()+":"+s.getStudentname());
				
				 item.getChildren().add(sItem);
				
				 System.out.println(item.getChildren().get(index));
				index++;
			 }
		 }
			 else if(Selectstudentorteacher==1) {
				 
				 Tlist=teacherDAO.getall();
				 for(Teacher t:Tlist) {
					 sItem=new TreeItem<String>(t.getTeacherid()+":"+t.getTeachername());
					 item.getChildren().add(sItem);
				 } 
			 
		 } 
		
		 treeview.setRoot(item)	;
		 
		listent();
		 
	}
	
	public void listent() {
		 treeview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
				@Override
				public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue,
						TreeItem<String> newValue) {
					
					 selecttiem=newValue;
					 String[] s=selecttiem.getValue().split(":");
					studentid.setText(s[0]);
					studentname.setText(s[1]);
				}
			});
	}
	
	
	
	public seeviewController() {
		// TODO Auto-generated constructor stub
	}
	
	
	
	//加对象
	
	public void add() {
		if(Selectstudentorteacher==0) {
			String stuname=addnametext.getText();
			Student student=new Student();
			student.setStudentname(stuname);
			studentDAO.add(student);
			TreeItem<String> sitem=new TreeItem<>(student.getStudentid()+":"+student.getStudentname());
			item.getChildren().add(sitem);
			treeview.setRoot(item);
		}
		
		if(Selectstudentorteacher==1) {
			String teaname=addnametext.getText();
			Teacher Teacher=new Teacher();
			Teacher.setTeachername(teaname);
			teacherDAO.add(Teacher);
			TreeItem<String> sitem=new TreeItem<>(Teacher.getTeacherid()+":"+Teacher.getTeachername());
			item.getChildren().add(sitem);
			treeview.setRoot(item);
		}
	}
	
	//看细节
	
	public void seedetail() {
		Stage stage=new Stage();
		
		try {
			studentids=studentid.getText();
			Parent root=FXMLLoader.load(getClass().getResource("seeDetail.fxml"));
			stage.setTitle("detail");
			Scene scene=new Scene(root);
			stage.setScene(scene);
			stage.show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//删对象
	
	public void det() {
		if(Selectstudentorteacher==0) {
			String stuid=studentid.getText();
			
			studentDAO.del(Integer.parseInt(stuid));
			
			item.getChildren().remove(selecttiem);
			treeview.setRoot(item);
		}
		
		if(Selectstudentorteacher==1) {
			String teaid=studentid.getText();
			
			teacherDAO.del(Integer.parseInt(teaid));
			
			item.getChildren().remove(selecttiem);
			treeview.setRoot(item);
		}
	}
	
	//监听选择
	
	private TreeItem<String> selecttiem;
	//监听树表点击
			
			

}
